package com.servlet;

import com.util.UtilJdbc;
import pom.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("service");

        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String name = req.getParameter("uname");
        String password = req.getParameter("upassword");
        System.out.println("======" + name + "======" + password);

        UtilJdbc utilJdbc = new UtilJdbc();
        ResultSet userSet = utilJdbc.login(name, password);
        try {
            if (userSet.next()) {

                    Integer userid = userSet.getInt("id");
                    System.out.println(userSet.getInt("id"));
                    System.out.println("查找到的id");

                    req.getRequestDispatcher("HomePage.jsp").forward(req, resp);


            } else {
                req.getRequestDispatcher("index.jsp").forward(req, resp);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
